<?php

/**********************\

File     :	.php
Created  :	
Updated  :  
Author   :	
Function :	
Comments :	

\**********************/

switch($task) {
	case 'remove':
	case 'delete':
		casa_delete(  $option,  intval( $cid[0] ) );
	case 'orderup':
		casa_order(  intval( $cid[0] ), $p_id,  -1, $option );
		break;
	case 'orderdown':
		casa_order(  intval( $cid[0] ), $p_id,  1, $option );
		break;
	case "search":
		break;
	case "new":
	case "edit":
		casa_edit( $option,  intval( $cid[0] ), $p_id);
		break;
	case "save":
		casa_save( $option, $c_id );
		break;
	case "list":
	default:
		casa_list( $option , $p_id);
		break;
}


/**
* list of houses.
* require province param
*/

function casa_list( $option, $p_id ) {
	global $my, $access, $database;

	$limit 		= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
	$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );

	$query = "SELECT count(*) FROM #__congregation_casa WHERE p_id='$p_id'";

	$database->setQuery( $query );
	$total = $database->loadResult();
	
	if ( $total <= $limit ) $limitstart = 0;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );

	$pageNav = new mosPageNav( $total, $limitstart, $limit  );


	$row = new congregationProvince( $database );
	$row->load( $p_id );
	
	$query = "SELECT #__congregation_casa.*, count(#__congregation_profile.username) AS c_members "
	. "\nFROM #__congregation_casa "
	. "\nLEFT JOIN #__congregation_profile "
	. "\nON #__congregation_casa.c_id=#__congregation_profile.c_id "
	. "\nWHERE #__congregation_casa.p_id='$row->p_id' "
	. "\nGROUP BY #__congregation_casa.c_id "
	. "\nORDER BY #__congregation_casa.ordering "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;

	$database->setQuery( $query );

	$row->case = $database->loadObjectList();

	HTML_casa::casa_list( $row, $option, $pageNav );
}



/**
* Moves the order of a record
* @param integer The increment to reorder by
*/
function casa_order( $uid, $p_id, $inc, $option ) {
	global $database, $rub, $Itemid;

	$row = new congregationCasa( $database );
	$row->load( $uid );
	$row->id = $row->c_id;
	$row->move( $inc, "p_id = $p_id" );

	mosRedirect( ("?option=$option&rub=casa&p_id=$p_id&Itemid=$Itemid") );
}

function casa_delete(  $option, $c_id ) {
	global $database, $access;

	$row = new congregationCasa( $database );
	$row->load( $c_id );

	if(!$row->c_id) {
		echo "<script> alert('". _Casa_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "SELECT count(*) FROM #__congregation_profile WHERE c_id='$row->c_id'";
	$database->setQuery( $query );

	$members = $database->loadResult();

	if($members > 0 ) {
		echo "<script> alert('". _Casa_Has_Members ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "DELETE FROM #__congregation_casa"
	. "\n WHERE c_id = '$c_id' "
	;
	$database->setQuery( $query );
	if (!$database->query()) {
		echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
	}
	mosRedirect( ("?option=$option&rub=casa&p_id=$row->p_id"));
	
	
}

function casa_view( $option, $c_id ) {
	global $database, $my;
	if(!$my->username) {
		mosNotAuth();
		return;
	}

	$row = new congregationCasa( $database );
	$row->load( $c_id );

	if(!$row->c_id) {
		echo "<script> alert('". _Casa_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}

	$query = "SELECT p_name FROM #__congregation_province WHERE p_id='$row->p_id'";
	$database->setQuery( $query );

	$row->c_pname = $database->loadResult();

	$query = "SELECT #__users.username as lusername, #__users.email as lemail, #__congregation_profile.* FROM #__users, #__congregation_profile "
	. "\nWHERE #__users.username=#__congregation_profile.username AND #__congregation_profile.c_id='$c_id'";

	$database->setQuery( $query );
	$row->c_members = $database->loadObjectList();

  HTML_casa::casa_view( $row, $option );


}



function casa_save( $option, $c_id ) {
	global $Itemid, $rub, $database;
	global $database, $my, $mosConfig_absolute_path, $mosConfig_live_site;

	$row = new congregationCasa( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}


///// sary insertion
$imagefile_name = $_FILES['imagefile']['tmp_name'];
$imagefile = $_FILES['imagefile']['name'];

	if (@is_uploaded_file($imagefile_name)) {
		
		if (!eregi("(\.jpg|\.jpeg|\.png|\.gif)$", $imagefile)){
			$errmsg .= "<li> ". _Congregation_Image_Error . " " . $imagefile ."</li>";
		} else {
			// photodir
			if(is_dir($mosConfig_absolute_path . "/media/casa")) {
				$photodir = $mosConfig_absolute_path . "/media/casa/";
				$photohtml = $mosConfig_live_site . "/media/casa/";
			} elseif(mkdir($mosConfig_absolute_path . "/media/casa")) {
				$photodir = $mosConfig_absolute_path . "/media/casa/";
				$photohtml = $mosConfig_live_site . "/media/casa/";
			} 



			if(@move_uploaded_file($imagefile_name, $photodir . $imagefile)) {
				$size = GetImageSize($photodir . $imagefile);
				$imagewidth = $size[0];
				$imageheight = $size[1];
				$imagetype = $size[2];
				
				if($imagewidth > 300) {
					$new_w = 300;
					$ratio = ($imagewidth / $new_w);

					$new_h = round($imageheight / $ratio);
					$pic_thumb = 1;

					if ($imagetype=="2") {
					$src_img = imagecreatefromjpeg($photodir . $imagefile);
					$dst_img = ImageCreateTrueColor($new_w,$new_h);
					imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img), imagesy($src_img));
					imagejpeg($dst_img, $photodir . "s".$imagefile);
					} elseif  ($imagetype=="3") {
					$src_img=ImageCreateFrompng($photodir . $imagefile);
					$dst_img=ImageCreateTrueColor($new_w,$new_h);
					ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img), ImageSY($src_img));
					Imagepng($dst_img, $photodir . "s".$imagefile);
					}
					
				}
				$new_w = $imagewidth;
				$new_h = $imageheight;
				$pic_thumb = 1;

				//unlink($photodir . $saryfile);
				
				$row->c_picfile = $imagefile;
			} else {
				$errmsg .= "<li> ". _Congregation_Image_Error ."</li>";
			}
		}
	}
///// sary insertion end



	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	
	$row->updateOrder(" p_id='$row->p_id' ");

	mosRedirect( ("?option=$option&rub=casa&p_id=$row->p_id"), "Saved" );


}

function casa_edit( $option, $c_id, $p_id = "" ) {
	global $my, $access, $database;

	// load provinces

	$query  = "SELECT * FROM #__congregation_province ORDER BY ordering"; 
	$database->setQuery( $query );
	$provinces = $database->loadObjectList();
	
	$row = new congregationCasa( $database );
	$row->load( $c_id );

	$lists = array();

	// build the html select list for ordering
	$query = "SELECT ordering AS value, c_name AS text"
	. "\n FROM #__congregation_casa"
	. "\n ORDER BY ordering "
	;
	$lists['ordering'] 		= mosAdminMenus::SpecificOrdering( $row, $c_id, $query, 1 );

	if($p_id != "") {
		$row->p_id = $p_id;
	}

  HTML_casa::casa_edit( $row, $provinces, $lists, $option );
}

class HTML_casa {
	
//Casa

	function casa_edit( &$row, &$provinces, &$lists, $option ) {
		global $params, $Itemid;
			
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Casa_Edit; ?>
			</th>
		</tr>
		</table>
			<form method="post" action="" enctype="multipart/form-data" name="adminForm">
					<input type="hidden" name="c_id" value="<?php echo $row->c_id ;?>" />
					<input type="hidden" name="option" value="<?php echo $option ;?>" />
					<input type="hidden" name="rub" value="casa" />
					<input type="hidden" name="task" value="save" />
			<table class="adminform">
				<tr valign="top">
					<td><?php echo _Casa_Name ; ?> : </td>
					<td><input type="text" name="c_name"  style="width: 300px" value="<?php echo $row->c_name ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Order ; ?> : </td>
					<td><?php echo $lists['ordering'] ;?></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Province ; ?> : </td>
					<td><select name="p_id"  style="width: 300px"/>
						<option />
						<?php
							if($provinces) {
								foreach($provinces as $province) {
									$selected = ($row->p_id == $province->p_id) ? " selected " : "";
									echo "<option value='" . $province->p_id . "' $selected >" . $province->p_name . "</option>";
								}
							} 
						?>
						</select>
					</td>
				</tr>
				<tr valign="top">
				  <td>
						<?php echo _Casa_Photo ;?> : </b>
				  </td>
				  <td>
				   <input type='file' size='30' name='imagefile' style='width: 300px'><br>
				  </td>
				 </tr>
				<tr valign="top">
					<td><?php echo _Casa_Address ; ?> : </td>
					<td><textarea name="c_address" rows="6" style="width: 300px"><?php echo $row->c_address ; ?></textarea></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Zip ; ?> : </td>
					<td><input type="text" name="c_zip"  style="width: 300px" value="<?php echo $row->c_zip ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_City ; ?> : </td>
					<td><input type="text" name="c_city"  style="width: 300px" value="<?php echo $row->c_city ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_State ; ?> : </td>
					<td><input type="text" name="c_state"  style="width: 300px" value="<?php echo $row->c_state ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Country ; ?> : </td>
					<td><select name="c_country"  style="width: 300px"/>
						<option><?php echo $row->c_country ;?></option>
<OPTION>Afghanistan<OPTION>Albania<OPTION>Algeria<OPTION>American Samoa<OPTION>Andorra<OPTION>Angola<OPTION>Anguilla<OPTION>Antarctica<OPTION>Antigua and arbuda<OPTION>Argentina<OPTION>Armenia<OPTION>Aruba<OPTION>Australia<OPTION>Austria<OPTION>Azerbaijan<OPTION>Bahamas<OPTION>Bahrain<OPTION>Bangladesh<OPTION>Barbados<OPTION>Belarus<OPTION>Belgium<OPTION>Belize<OPTION>Benin<OPTION>Bermuda<OPTION>Bhutan<OPTION>Bolivia<OPTION>Bosnia and Herzegovina<OPTION>Botswana<OPTION>Bouvet Island<OPTION>Brazil<OPTION>British Indian Ocean Territory<OPTION>Brunei Darussalam<OPTION>Bulgaria<OPTION>Burkina Faso<OPTION>Burundi<OPTION>Cambodia<OPTION>Cameroon<OPTION>Canada<OPTION>Cape Verde<OPTION>Cayman Islands<OPTION>Central African Republic<OPTION>Chad<OPTION>Chile<OPTION>China<OPTION>Christmas Island<OPTION>Cocos Keeling Islands<OPTION>Colombia<OPTION>Comoros<OPTION>Congo<OPTION>Cook Islands<OPTION>Costa Rica<OPTION>Ivory Coast<OPTION>Croatia<OPTION>Cuba<OPTION>Cyprus<OPTION>Czech Republic<OPTION>Denmark<OPTION>Djibouti<OPTION>Dominican Republic<OPTION>Dominica<OPTION>East Timor<OPTION>Ecuador<OPTION>Egypt<OPTION>El Salvador<OPTION>Equatorial Guinea<OPTION>Eritrea<OPTION>Estonia<OPTION>Ethiopia<OPTION>Falkland Islands<OPTION>Faroe Islands<OPTION>Fiji<OPTION>Finland<OPTION>France<OPTION>French Guiana<OPTION>French Polynesia<OPTION>French Southern Territories<OPTION>Gabon<OPTION>Gambia<OPTION>Georgia<OPTION>Germany<OPTION>Ghana<OPTION>Gibraltar<OPTION>Greece<OPTION>Greenland<OPTION>Grenada<OPTION>Guadeloupe<OPTION>Guam<OPTION>Guatemala<OPTION>Guinea-Bissau<OPTION>Guinea<OPTION>Guyana<OPTION>Haiti<OPTION>Heard and McDonald Islands<OPTION>Honduras<OPTION>Hong Kong<OPTION>Hungary<OPTION>Iceland<OPTION>India<OPTION>Indonesia<OPTION>Iran<OPTION>Iraq<OPTION>Ireland<OPTION>Israel<OPTION>Italy<OPTION>Jamaica<OPTION>Japan<OPTION>Jordan<OPTION>Kazakhstan<OPTION>Kenya<OPTION>Kiribati<OPTION>North Korea<OPTION>South Korea<OPTION>Kuwait<OPTION>Kyrgyzstan<OPTION>Laos<OPTION>Latvia<OPTION>Lebanon<OPTION>Lesotho<OPTION>Liberia<OPTION>Libya<OPTION>Liechtenstein<OPTION>Lithuania<OPTION>Luxembourg<OPTION>Macau<OPTION>Macedonia<OPTION>Madagascar<OPTION>Malawi<OPTION>Malaysia<OPTION>Maldives<OPTION>Mali<OPTION>Malta<OPTION>Marshall slands<OPTION>Martinique<OPTION>Mauritania<OPTION>Mauritius<OPTION>Mayotte<OPTION>Mexico<OPTION>Micronesia<OPTION>Moldova<OPTION>Monaco<OPTION>Mongolia<OPTION>Montserrat<OPTION>Morocco<OPTION>Mozambique<OPTION>Myanmar<OPTION>Namibia<OPTION>Nauru<OPTION>Nepal<OPTION>Netherlands Antilles<OPTION>Netherlands<OPTION>New Caledonia<OPTION>New Zealand<OPTION>Nicaragua<OPTION>Nigeria<OPTION>Niger<OPTION>Niue<OPTION>Norfolk Island<OPTION>Northern Mariana slands<OPTION>Norway<OPTION>Oman<OPTION>Pakistan<OPTION>Palau<OPTION>Panama<OPTION>Papua New Guinea<OPTION>Paraguay<OPTION>Peru<OPTION>Philippines<OPTION>Pitcairn<OPTION>Poland<OPTION>Portugal<OPTION>Puerto Rico<OPTION>Qatar<OPTION>Reunion<OPTION>Romania<OPTION>Russian Federation<OPTION>Rwanda<OPTION>S Georgia and S Sandwich Isles<OPTION>Saint Kitts and Nevis<OPTION>Saint Lucia<OPTION>Saint Vincent and The Grenadines<OPTION>Samoa<OPTION>San Marino<OPTION>Sao Tome and Principe<OPTION>Saudi Arabia<OPTION>Senegal<OPTION>Seychelles<OPTION>Sierra Leone<OPTION>Singapore<OPTION>Slovak Republic<OPTION>Slovenia<OPTION>Solomon Islands<OPTION>Somalia<OPTION>South Africa<OPTION>Spain<OPTION>Sri Lanka<OPTION>St. Helena<OPTION>St. Pierre and Miquelon<OPTION>Sudan<OPTION>Suriname<OPTION>Svalbard and Jan Mayen Islands<OPTION>Swaziland<OPTION>Sweden<OPTION>Switzerland<OPTION>Syria<OPTION>Taiwan<OPTION>Tajikistan<OPTION>Tanzania<OPTION>Thailand<OPTION>Togo<OPTION>Tokelau<OPTION>Tonga<OPTION>Trinidad and Tobago<OPTION>Tunisia<OPTION>Turkey<OPTION>Turkmenistan<OPTION>Turks and Caicos Islands<OPTION>Tuvalu<OPTION>US Minor Outlying Islands<OPTION>Uganda<OPTION>Ukraine<OPTION>United Arab Emirates<OPTION>United Kingdom<OPTION>United States<OPTION>Uruguay<OPTION>Uzbekistan<OPTION>Vanuatu<OPTION>Vatican City State<OPTION>Venezuela<OPTION>Viet Nam<OPTION>British Virgin Islands<OPTION>US Virgin Islands<OPTION>Wallis and Futuna Islands<OPTION>Western Sahara<OPTION>Yemen<OPTION>Yugoslavia<OPTION>Zaire<OPTION>Zambia<OPTION>Zimbabwe</select>
						</select>
					</td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Tel ; ?> : </td>
					<td><input type="text" name="c_tel"  style="width: 300px" value="<?php echo $row->c_tel ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Fax ; ?> : </td>
					<td><input type="text" name="c_fax"  style="width: 300px" value="<?php echo $row->c_fax ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Email ; ?> : </td>
					<td><textarea name="c_email"  rows="5" style="width: 300px"><?php echo $row->c_email ; ?></textarea></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Web ; ?> : </td>
					<td><input type="text" name="c_web"  style="width: 300px" value="<?php echo $row->c_web ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Foundation ; ?> : </td>
					<td><input type="text" name="c_found"  style="width: 300px" value="<?php echo $row->c_web ; ?>"/></td>
				</tr>
				<tr valign="top">
					<td><?php echo _Casa_Diocese ; ?> : </td>
					<td><input type="text" name="c_diocese"  style="width: 300px" value="<?php echo $row->c_diocese ; ?>"/></td>
				</tr>

				</tr>
			</table>
			</form>
	<?php
	}



	function casa_list( &$row, $option , &$pageNav) {
		global $Itemid;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo $row->p_name ; ?>
			</th>
		</tr>
		</table>
		<form action="index2.php" method="post" name="adminForm">

		<table class="adminlist">
				<tr valign="top">
				<th width="20">
				#
				</th>
				<th width="20">
				<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $row->case ); ?>);" />
				</th>
					<th><?php echo _Casa_Name ; ?> </th>
				<th colspan="2" width="5%">
				<?php echo _E_ORDERING; ?>
				</th>
					<th><?php echo _Casa_Members ; ?></th>

				</tr>
				<?php
				if($row->case) {
				$k = 0;
				for ($i=0, $n=count( $row->case ); $i < $n; $i++) {
					$casa = &$row->case[$i];
					$k = 1 - $k;
					
					$casa->id = $casa->c_id;
					$checked 	= mosCommonHTML::CheckedOutProcessing( $casa, $i );

					$link 		= 'index2.php?option=com_congregation&rub=casa&task=edit&hidemainmenu=1&id='. $casa->c_id;
						?>
			<tr class="<?php echo "row$k"; ?>">
				<td  width="20">
				<?php echo $pageNav->rowNumber( $i ); ?>
				</td>
				<td  width="20">
				<?php echo $checked; ?>
				</td>
					<td><?php echo $casa->c_name . " " . $casa->c_city . " (" . $casa->c_state . ")" ; ?> </td>

				<td align="center">
				<?php echo $pageNav->orderUpIcon( $i ); ?>
				</td>
				<td align="center">
				<?php echo $pageNav->orderDownIcon( $i, $n ); ?>
				</td>



					<td align="center"><?php echo $casa->c_members . '<a href="' . ( "?option=$option&rub=profile&Itemid=$Itemid&task=view&c_id=" . $casa->c_id ) . '"> (' . _Congregation_View . ')</a>'; ?></td>
				
				</tr>
						<?php
					}
				}
				?>
			</table>
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="rub" value="casa" />
		<input type="hidden" name="p_id" value="<?php echo $row->p_id ;?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="hidemainmenu" value="0" />
		<?php
		echo $pageNav->getListFooter(); 
		?>
			</form>
<?php
	}



}
?>
